package com.lh.serverforwebrtc.websocket.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lh.serverforwebrtc.bean.RespBean;
import com.lh.serverforwebrtc.websocket.server.WebSocketServer;

import javax.websocket.Session;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Lh
 * @Description:
 * @Date: Created in 9:14 2022/6/13
 */
public class MsgHandler {

    private WebSocketServer webSocketServer;

    public MsgHandler(WebSocketServer webSocketServer) {
        this.webSocketServer = webSocketServer;
    }

    public void handleMessage(String message,String id,Session session){
        try {
            JSONObject jsonObject = JSON.parseObject(message);
            if (jsonObject != null){
                switch (jsonObject.getString("action")){
                    case "enter_room":
                        webSocketServer.enterRoom(jsonObject);
                        break;
                    case "send_offer":
                    case "send_answer":
                        webSocketServer.appointSend(jsonObject.getString("receiver"),message);
                        break;
                    case "send_iceCandidate":
                        webSocketServer.sendIceCandidate(jsonObject);
                        break;
                    case "leave_room":
                        webSocketServer.handleLeave(jsonObject);
                        break;
                    case "send_call":
                        webSocketServer.sendCall(jsonObject);
                        break;
                    case "answer_call":
                        webSocketServer.handleAnswerCall(jsonObject);
                        break;
                    case "send_gift":
                        webSocketServer.sendGiftMsg(jsonObject);
                        break;
                    case "hang_up":
                        webSocketServer.handleHangUp(jsonObject);
                        break;
                    //"action":"chat_message","content":"gggggggggggggggw","roomId":"511152","sender":"9010","username":"用户9010"
                    case "chat_message":
                        webSocketServer.handleChatMessage(jsonObject);
                        break;

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
